package barcode.android.dao.impl;

import junit.framework.Assert;
import barcode.android.activity.Barcode_androidActivity;
import barcode.android.dao.DormDao;
import barcode.android.dao.impl.BaseDaoImpl;
import barcode.android.dao.impl.DormDaoImpl;
import barcode.android.dao.impl.SqliteDatabaseState;
import barcode.android.model.Dorm;
import android.test.ActivityInstrumentationTestCase2;

public class DormDaoTest extends
		ActivityInstrumentationTestCase2<Barcode_androidActivity> {

	public DormDaoTest() {
		super(Barcode_androidActivity.class);
	}

	public void setUp() {
		BaseDaoImpl.initContext(getActivity().getBaseContext());
	}

	public void testAddDorm() {
		Dorm dorm = new Dorm();
		dorm.setName("韵苑16#");

		DormDao dao = new DormDaoImpl();
		try {
			dao.beginTransaction(SqliteDatabaseState.Writable);
			dao.add(dorm);
			dao.setTransactionSuccessful();
		} finally {
			dao.endTransaction();
		}
	}

	public void testQueryDorm() {
		DormDao dao = new DormDaoImpl();
		Dorm dorm;
		try {
			dao.beginTransaction(SqliteDatabaseState.Readable);
			dorm = dao.query(3);
			dao.setTransactionSuccessful();
		} finally {
			dao.endTransaction();
		}
		Assert.assertEquals("韵苑16#", dorm.getName());
	}

	public void testDeleteDorm() {
		DormDao dao = new DormDaoImpl();
		try {
			dao.beginTransaction(SqliteDatabaseState.Writable);
			dao.delete(1);
			dao.setTransactionSuccessful();
		} finally {
			dao.endTransaction();
		}
	}

	public void testUpdateDorm() {
		Dorm dorm = new Dorm();
		dorm.setId(2);
		dorm.setName("紫崧4#");

		DormDao dao = new DormDaoImpl();
		try {
			dao.beginTransaction(SqliteDatabaseState.Writable);
			dao.update(dorm);
			dao.setTransactionSuccessful();
		} finally {
			dao.endTransaction();
		}

		try {
			dao.beginTransaction(SqliteDatabaseState.Readable);
			dorm = dao.query(2);
			dao.setTransactionSuccessful();
		} finally {
			dao.endTransaction();
		}
		Assert.assertEquals("紫崧4#", dorm.getName());
	}
}
